package com.orangelife.service.propFeeTransfer.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.propFeeTransfer.IPropFeeTransferService;
import com.orangelife.service.propFeeTransfer.model.PropFeeTransferDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.StringUtils;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation CodeConst.BIS_CODE_68001015. All Rights
 * Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * PropFeeTransfer类的 service层的增删改查
 * 
 * @author @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2017-01-03 11:43:14
 * ==============================================================
 */
@Service("propFeeTransferService")
public class PropFeeTransferServiceImpl extends MybaitsGenericDaoSubLogic
		implements IPropFeeTransferService {
	private static Logger logger = Logger
			.getLogger(PropFeeTransferServiceImpl.class);

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PropFeeTransferServiceImpl.logger = logger;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeeTransfer(PropFeeTransferDto propFeeTransfer)
			throws AppBizException {

		Assert.notNull(propFeeTransfer);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		if (!StringUtils.isBlank(propFeeTransfer.getReceiptCode())) {
			if (propFeeTransfer.getReceiptCode().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "收据编码长度超出");
			}
		}

		propFeeTransfer.setTransferID(IdUtil.getuuId());
		propFeeTransfer.setItemStat(TypeConst.FLAG_ON);
		propFeeTransfer.setTscreate(new Date());
		propFeeTransfer.setTsedit(new Date());
		return this.getSqlSession().insert(
				"PropFeeTransferMapper.insertSelective", propFeeTransfer);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editPropFeeTransfer(PropFeeTransferDto propFeeTransfer)
			throws AppBizException {

		Assert.notNull(propFeeTransfer);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeeTransfer.getTransferID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeeTransferDto oldPropFeeTransfer = this
				.findPropFeeTransferByID(propFeeTransfer.getTransferID());

		if (!StringUtils.isBlank(propFeeTransfer.getReceiptCode())) {
			if (propFeeTransfer.getReceiptCode().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "收据编码长度超出");
			}
		}

		if (oldPropFeeTransfer == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		propFeeTransfer.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeTransferMapper.updateByPrimaryKeySelective",
				propFeeTransfer);
	}

	@Override
	public PropFeeTransferDto findPropFeeTransferByID(String propFeeTransferID)
			throws AppBizException {
		if (StringUtils.isBlank(propFeeTransferID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeTransferDto oldPropFeeTransfer = this.getSqlSession().selectOne(
				"PropFeeTransferSearchMapper.selectByPrimaryKey",
				propFeeTransferID);
		if (oldPropFeeTransfer == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		if (TypeConst.FLAG_OFF.equals(oldPropFeeTransfer.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		return oldPropFeeTransfer;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int deletePropFeeTransferByLogic(String propFeeTransferID)
			throws AppBizException {

		if (StringUtils.isBlank(propFeeTransferID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeTransferDto oldPropFeeTransfer = this
				.findPropFeeTransferByID(propFeeTransferID);

		oldPropFeeTransfer.setItemStat(TypeConst.FLAG_OFF);
		oldPropFeeTransfer.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeTransferMapper.updateByPrimaryKeySelective",
				oldPropFeeTransfer);
	}

	@Override
	public List<Map<String, Object>> findPropFeeTransferByPage(String tsCreate,
			String search, int pageNo, int pageSize) throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeTransferSearchMapper.findAllPropFeeTransfer", map);
		return list;
	}

	@Override
	public List<PropFeeTransferDto> getPropFeeTransferByPage(String tsCreate,
			String search, int pageNo, int pageSize) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<PropFeeTransferDto> list = this.getSqlSession().selectList(
				"PropFeeTransferSearchMapper.getAllPropFeeTransferDto", map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectPropFeeTransferByRelevantID(
			String transferID, String directBillID, String commID,
			String apartmentID) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("transferID", transferID);
		map.put("directBillID", directBillID);
		map.put("commID", commID);
		map.put("apartmentID", apartmentID);

		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeTransferSearchMapper.selectPropFeeTransferDtoyRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeTransferDto> findPropFeeTransferByRelevantID(
			String transferID, String directBillID, String commID,
			String apartmentID) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("transferID", transferID);
		map.put("directBillID", directBillID);
		map.put("commID", commID);
		map.put("apartmentID", apartmentID);

		List<PropFeeTransferDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeTransferSearchMapper.findPropFeeTransferDtoByRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeTransferDto> bauthfindPropFeeTransfer(
			List<String> propFeeTransferIDs) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<PropFeeTransferDto> list = this.getSqlSession().selectList(
				"PropFeeTransferSearchMapper.batchFindPropFeeTransfer",
				propFeeTransferIDs);
		return list;
	}

	@Override
	public List<Map<String, Object>> bauthfindPropFeeTransferByMap(
			Map<String, Object> propFeeTransfer) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeTransferSearchMapper.batchFindPropFeeTransferByMap",
				propFeeTransfer);
		return list;
	}

	@Override
	public void clearCache() throws AppBizException {
		System.out.println("清除缓存成功");
	}

}